package com.houdoudou.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.houdoudou.bean.Order;
import com.houdoudou.bean.OrderProduct;

public class OrderProductDao {


	 Connection conn=null;	


	public OrderProductDao(Connection conn) {
		super();
		this.conn = conn;
	}

	public  long create( OrderProduct orderProduct ) throws SQLException{	
		long result=0;
		  PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   String sql=null;
	
	        try {
	        	sql = "insert  into orderproduct (orderID,productID ,productType ,productName,branchID,"    
	   		+ "branchName,branchPrice ,branchVolume,branchNum,dir ) " //10个参数
	        + "values (?,?,?,?,?,?,?,?,?,?)";
	        	//共45个参数。
	        	pstm=conn.prepareStatement(sql);
	        	pstm.setLong(1, orderProduct.getOrderID());
	        	pstm.setInt(2, orderProduct.getProductID());        	
	        	pstm.setInt(3, orderProduct.getProductType()); 
	        	pstm.setString(4, orderProduct.getProductName());  
	        	pstm.setInt(5, orderProduct.getBranchID());  
	        	pstm.setString(6, orderProduct.getBranchName());
	        	pstm.setDouble(7, orderProduct.getBranchPrice());
	        	
	        	pstm.setInt(8, orderProduct.getBranchVolume());
	        	pstm.setInt(9, orderProduct.getBranchNum());
	        	pstm.setString(10, orderProduct.getDir());
				pstm.executeUpdate(); 	
				
			} finally{
				ConnFactory.closeStatment(pstm, pstm, rs);
			}			
		return result;
	}
	
	
	 public List<OrderProduct> selectById(Long orderID) throws Exception{
		  PreparedStatement pstm=null; 
		   Statement  stm=null;
		   ResultSet rs=null; 	
		   List<OrderProduct> orderProducts= new ArrayList<OrderProduct>();
	        try {   
	        	 String sql="select * from orderproduct  where orderID = "+ orderID;	  
	       
				stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
				rs=stm.executeQuery(sql);		
		        while(rs.next()){
		        	OrderProduct orderProduct= new OrderProduct();
		        	orderProduct.setOrderID(rs.getLong("orderID"));
		        	orderProduct.setProductID(rs.getInt("productID"));        	
		        	orderProduct.setProductType(rs.getInt("productType")); 
		        	orderProduct.setProductName(rs.getString("productName"));  
		        	orderProduct.setBranchID(rs.getInt("branchID"));  
		        	orderProduct.setBranchName(rs.getString("branchName"));
		        	orderProduct.setBranchPrice(rs.getDouble("branchPrice"));
		        	
		        	orderProduct.setBranchVolume(rs.getInt("branchVolume"));
		        	orderProduct.setBranchNum(rs.getInt("branchNum"));
		        	orderProduct.setDir(rs.getString("dir"));
		        	 orderProducts.add(orderProduct);
		        }
		 
	        }finally{
				ConnFactory.closeStatment(pstm, pstm, rs);
			}
	        
		   return orderProducts;
	   }
}
